package com.louis.resource;

import org.springframework.core.io.FileSystemResource;

import java.io.InputStream;

/**
 * @author XRY
 * @date 2023年06月28日19:31
 */

//访问系统资源
public class FileSystemResourceDemo {
    public static void  loadFileResource(String path){
        //创建对象
        FileSystemResource fileResource = new FileSystemResource(path);
        System.out.println("fileResource.getFilename() = " + fileResource.getFilename());
        System.out.println("fileResource.getDescription() = " + fileResource.getDescription());
        //创建输入流对象
        try {
            InputStream inputStream = fileResource.getInputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while((len=inputStream.read(bytes))!=-1){
                System.out.println(new String(bytes, 0, len));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //相对路径或绝对路径
        loadFileResource("C:\\Users\\XRY\\Desktop\\text.txt");
        /*
        *   fileResource.getFilename() = text.txt
            fileResource.getDescription() = file [C:\Users\XRY\Desktop\text.txt]
            测试fileSystemResource
        * */
    }
}
